package com.geeklan.smart.ui.dialog;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.viewbinding.ViewBinding;

import com.geeklan.smart.R;
import com.geeklan.smart.databinding.DialogBaseBinding;

public abstract class BaseAlertDialog<VB extends ViewBinding> extends AlertDialog {

    protected VB mBinding;
    private DialogBaseBinding baseBinding;

    public BaseAlertDialog(@NonNull Context context) {
        super(context,R.style.CustomDialogTheme);
        initDialog();
    }

    private void initDialog() {
        baseBinding = DialogBaseBinding.inflate(LayoutInflater.from(getContext()));
        setView(baseBinding.getRoot());

        mBinding = getContentViewBinding(LayoutInflater.from(getContext()));
        if (mBinding != null) {
            baseBinding.contentContainer.addView(mBinding.getRoot());
        }

        baseBinding.btnPositive.setOnClickListener(v -> onPositiveButtonClick());
        baseBinding.btnNegative.setOnClickListener(v -> onNegativeButtonClick());
    }
    public void setTopCloseButtonVisibility(boolean isVisibility){
        baseBinding.btnClose.setVisibility(isVisibility ? View.VISIBLE:View.GONE);
        baseBinding.btnClose.setOnClickListener(v -> dismiss());
    }
    public void setShowRelativeLayoutButton(View.OnClickListener okOnClickListener,View.OnClickListener cancelOnClickListener){
        baseBinding.rlButton.setVisibility(View.VISIBLE);
        baseBinding.tvOk.setOnClickListener(okOnClickListener);
        baseBinding.tvCancel.setOnClickListener(cancelOnClickListener);
    }

    public void setTitle(String title) {
        baseBinding.tvTitle.setText(title);
    }

    public void setButtonText(String text){
        if (!TextUtils.isEmpty(text)) {
            baseBinding.btnOk.setText(text);
            baseBinding.btnOk.setVisibility(View.VISIBLE);
        }
    }

    public void setOnButtonClickListener(View.OnClickListener listener) {
        baseBinding.btnOk.setOnClickListener(listener);
    }

    public void setPositiveButtonText(String text) {
        if (!TextUtils.isEmpty(text)) {
            baseBinding.btnPositive.setText(text);
            baseBinding.btnPositive.setVisibility(View.VISIBLE);
        }
    }

    public void setNegativeButtonText(String text) {
        if (!TextUtils.isEmpty(text)) {
            baseBinding.btnNegative.setText(text);
            baseBinding.btnNegative.setVisibility(View.VISIBLE);
        }
    }

    public void setOnPositiveButtonClickListener(View.OnClickListener listener) {
        baseBinding.btnPositive.setOnClickListener(listener);
    }

    public void setOnNegativeButtonClickListener(View.OnClickListener listener) {
        baseBinding.btnNegative.setOnClickListener(listener);
    }

    protected abstract VB getContentViewBinding(LayoutInflater inflater);

    protected void onPositiveButtonClick() {
        dismiss();
    }

    protected void onNegativeButtonClick() {
        dismiss();
    }
}